package com.ruoyi.web.controller.DataStatistics;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.DataStatistics;
import com.ruoyi.system.domain.vo.Datastatisticsvo;
import com.ruoyi.system.service.IEmpcountService;
import com.ruoyi.system.domain.vo.EpmInformationHistoryVo;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@RestController
@RequestMapping("/Empment/dataStatistics")
@Slf4j
public class DataStatisticsController  extends BaseController {
    @Autowired
    private IEmpcountService iEmpcountService;
    /**
     * 获取组织机构树
     * @return
     */
    /*
    * 按各部门进行人数统计
    * */
    @PreAuthorize("@ss.hasPermi('Empment:dataStatistics:list')")
    @GetMapping("/count")
//    默认传参为100
    public AjaxResult getEmpnum(@PathVariable(value = "deptId", required = false) Long deptId){
        List<Datastatisticsvo> deptVoList = iEmpcountService.Empcount();
        return AjaxResult.success(deptVoList);

    }
    @PreAuthorize("@ss.hasPermi('Empment:dataStatistics:list')")
    @GetMapping("/count/{date}")
    public AjaxResult getEmpnumByList(String date){
        iEmpcountService.Empcount2(date);
        return AjaxResult.success();

    }

    @PreAuthorize("@ss.hasPermi('Empment:dataStatistics:list')")
    @GetMapping("/count/his/{date}")
    public AjaxResult getEmpnumByTime(@PathVariable(value = "date", required = true) String date){
        List<Datastatisticsvo> deptVoList = iEmpcountService.Empcount2(date);
        return AjaxResult.success(deptVoList);
    }
    /**
     * 入职与离职人数统计
     * @return
     */
    @PreAuthorize("@ss.hasPermi('Empment:dataStatistics:list')")
    @GetMapping("/operation/{range}")
    public AjaxResult getEmpOps( @PathVariable(value = "range", required = true) Integer  range){

        List<EpmInformationHistoryVo> epmInformationHistory = iEmpcountService.EmpOps(range);
        return AjaxResult.success(epmInformationHistory);

    }


    /**
     * 按部门和在职时间进行人数统计
     * @return
     */
    @PreAuthorize("@ss.hasPermi('Empment:dataStatistics:list')")
    @GetMapping("/xi/{minTime}/{maxTime}/{deptId}")
    public AjaxResult getEmpnumByTime(@PathVariable(value = "minTime", required = false) Integer minTime,
                                      @PathVariable(value = "maxTime", required = false) Integer maxTime,
                                      @PathVariable(value = "deptId", required = false) Long deptId){
        DataStatistics deptVoList = iEmpcountService.EmpcountByTime(minTime, maxTime,deptId);
        return AjaxResult.success(deptVoList);
    }

    @PreAuthorize("@ss.hasPermi('Empment:dataStatistics:list')")
    @GetMapping("/genderlist")
//    默认传参为100
    public AjaxResult getGenderList(@PathVariable(value = "deptId", required = false) Long deptId){
        List<Datastatisticsvo> deptVoList = iEmpcountService.getGenderList();
        return AjaxResult.success(deptVoList);

    }

    @PreAuthorize("@ss.hasPermi('Empment:dataStatistics:list')")
    @GetMapping("/educationalLeveList")
//    默认传参为100
    public AjaxResult getEducationalLeveList(@PathVariable(value = "deptId", required = false) Long deptId){
        List<Datastatisticsvo> deptVoList = iEmpcountService.getEducationalLeveList();
        return AjaxResult.success(deptVoList);

    }

    @PreAuthorize("@ss.hasPermi('Empment:dataStatistics:list')")
    @GetMapping("/regionList")
    public AjaxResult getRegionList(@PathVariable(value = "deptId", required = false) Long deptId){
        List<Datastatisticsvo> deptVoList = iEmpcountService.getRegionList();
        return AjaxResult.success(deptVoList);

    }

    /*
    * 按岗位查询
    */
    @PreAuthorize("@ss.hasPermi('Empment:dataStatistics:list')")
    @GetMapping("/gangweiList")
    public AjaxResult getGangweiList(@PathVariable(value = "deptId", required = false) Long deptId){
        List<Datastatisticsvo> deptVoList = iEmpcountService.getGangweiList();
        return AjaxResult.success(deptVoList);

    }

    /*
    * 按年龄统计
    * */
    @PreAuthorize("@ss.hasPermi('Empment:dataStatistics:list')")
    @GetMapping("/ageList")
    public AjaxResult getAgeList(@PathVariable(value = "deptId", required = false) Long deptId){
        List<Datastatisticsvo> deptVoList = iEmpcountService.getAgeList();
        return AjaxResult.success(deptVoList);
    }



    /**
     * 定时任务
     */
    @Scheduled(cron = "0 0 23 * * ?")
    public void scheduledTask() {
        log.info("=======定时任务开始执行=========");
        // 格式化输出
        LocalDateTime currentDateTime = LocalDateTime.now();
        System.out.println("当前日期和时间: " + currentDateTime);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String formattedDateTime = currentDateTime.format(formatter);        // 调用服务层方法进行数据统计
        iEmpcountService.resetList(iEmpcountService.Empcount(),formattedDateTime);
        log.info("===============定时任务执行完成================");
    }

}
